<?php
namespace app\service;
use think\Request,
	app\model\Order,
	app\model\Product,
	app\model\OrderList,
	app\model\User,
	app\validate\OrderValidate;

class InstorageService
{

    public function page(){

    	$data 	= Request::instance()->get();
    	$where 	= [];
    	//封装where查询条件
    	// empty($data['type']) 	|| $where['type'] 	= 	$data['type'];
    	// empty($data['author'])	|| $where['author']		= 	['like','%'.$data['author'] ];
    	// empty($data['sn'])		|| $where['sn'] 		= 	$data['sn'];
    	empty($data['sn'])	|| $where['sn']		= 	['like','%'.$data['sn'] ];
    	$where['state'] 		= 	'1';

    	$orderList  = Order::where($where)->paginate(10);
    	foreach ($orderList as $key => $val) {
    		$userName = User::get($val['author']);
    		if (!empty($userName)) {
    			$orderList[$key]['author'] = $userName['truename'];
    		}
    	}
        return $orderList;     
    }

    // 保存数据
    public function save(){
    	Request::instance()->isPost() || die('request not  post!');
    	
		$param = Request::instance()->param();	//获取参数
		$error = $this->_validate($param); 		// 是否通过验证


		if( is_null( $error ) ){

			$order 				= new Order();
			$order->sn 			= $param['sn'];
			// $order->type 		= $param['type'];
			// $order->desc 		= $param['desc'];
			$order->author 		= Session::get('uid');
			// $order->supplier 	= $param['supplier'];
			// $order->car_no 		= $param['car_no'];
			// $order->ban_no 		= $param['ban_no'];
			// $order->detailed_no = $param['detailed_no'];
			$order->state 		= 1;
			$order->add_time	= time();

			// 检测错误
			if( $order->save() ){
				$id = $order->id;
				//插入并更新
				$this->addList($param,$id);

				return ['error'	=>	0,'msg'	=>	'保存成功'];
			}else{
				return ['error'	=>	100,'msg'	=>	'保存失败'];	
			}
			
		}else{
			return ['error'	=>	100,'msg'	=>	$error];
		}

    }

    //插入列表
    public function addList($data,$order_id){

    	foreach ($data['product'] as $k=>$v) {

    		//id更新产品数量
			// $product = Product::get($v);
			// $product->num +=  $data['num'][$k];
			// $product->save();


			//添加到订单列表
			OrderList::create([
				'good_id'	 =>	$v,
				'order_id'	 =>	$order_id,
				'num'		 =>	$data['num'][$k],
				'storage_id' =>	$data['storage'][$k],
				'supplier'	 =>	$data['supplier'][$k],
				'cg_price'   =>	$data['cg_price'][$k],
				'out_order'	 =>	$data['out_order'][$k]
			]);
    	}

    }

    //更新列表
    public function addListUpdate($data,$order_id){

    	foreach ($data['product'] as $k=>$v) {
    		$orderList         = OrderList::get($data['listId'][$k]);
    		$orderList->good_id    = $v;
			$orderList->order_id   = $order_id;
			$orderList->num 	   = $data['num'][$k];
			$orderList->storage_id = $data['storage'][$k];
			$orderList->supplier   = $data['supplier'][$k];
			$orderList->cg_price   = $data['cg_price'][$k];
			$orderList->out_order  = $data['out_order'][$k];

			$a = $orderList->save();
    	}

    }

    public function getAllList($id){
    	return OrderList::All([
    		'order_id'	=> $id
    	]);
    }

    public function delete($id){
    	if( Order::destroy($id) ){
    		return ['error'	=>	0,'msg'	=>	'删除成功'];
    	}else{
    		return ['error'	=>	100,'msg'	=>	'删除失败'];	
    	}
    }

    public function edit($id){
    	return Order::get($id);
    }

    public function update(){
    	Request::instance()->isPost() || die('request not  post!');
    	
		$param = Request::instance()->param();	//获取参数

		$order 				 = Order::get($param['id']);

		if ($param['style'] == 'fh') {
			$order->desc  = $param['desc'];
			$order->image = $param['picture'];
			$order->is_fh = 1;

		} else {
			$order->check_status = $param['check_status'];
			$order->mark 		 = $param['mark'];
		}
		

		// 检测错误
		if( $order->save() ){
			if ($param['style'] != 'fh') {
				$this->addListUpdate($param,$order->id);
			}
			return ['error'	=>	0,'msg'	=>	'修改成功'];
		}else{
			return ['error'	=>	100,'msg'	=>	'修改失败'];	
		}

    }


    // 验证器
    private function _validate($data){
		// 验证
		$validate = validate('OrderValidate');
		if(!$validate->check($data)){
			return $validate->getError();
		}
    }

     public function upload()
    {
	    // 获取表单上传文件 例如上传了001.jpg
	    $files = request()->file();

	    // 移动到框架应用根目录/public/uploads/ 目录下
	    foreach($files as $file){
	        // 移动到框架应用根目录/public/uploads/ 目录下
	        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
	        if($info){
	            // 成功上传后 获取上传信息
	            // 输出 jpg
	            // echo $info->getExtension(); 
	            return json_encode($info->getSaveName()); 
	        }else{
	            // 上传失败获取错误信息
	            echo $file->getError();
	        }    
	    }
    }


}
